<?php

namespace App\Package\Finance;

use Exception;
use App\Models\Finance\Wages;
use Illuminate\Support\Facades\Log;

/**
 * 规则处理
 *
 * Date: 2018/6/13
 * @author George
 * @package App\Package\Finance
 */
class RegulationHandler
{
	/**
	 * 定义公司ID
	 *
	 * @var int $company_id
	 * Date: 2018/6/13
	 * @author George
	 */
	protected $company_id;

	/**
	 * 定义社保方案ID
	 *
	 * @var int $social_security_id
	 * Date: 2018/6/13
	 * @author George
	 */
	protected $social_security_id;

	/**
	 * 定义用户ID
	 *
	 * @var int $user_id
	 * Date: 2018/6/13
	 * @author George
	 */
	protected $user_id;

	/**
	 * 定义薪资方案ID
	 *
	 * @var int $pattern_id
	 * Date: 2018/6/13
	 * @author George
	 */
	protected $pattern_id;

	/**
	 * 定义出勤天数
	 *
	 * @var int $attendance_days
	 * Date: 2018/6/13
	 * @author George
	 */
	protected $attendance_days;

	/**
	 * 节假日加班天数
	 *
	 * @var int $holidays
	 * Date: 2018/6/13
	 * @author George
	 */
	protected $holidays;

	/**
	 * 定义存储自定义
	 *
	 * @var array $custom_fields
	 * Date: 2018/6/13
	 * @author George
	 */
	protected $custom_fields;

	/**
	 * @var
	 * Date: 2018/6/13
	 * @author George
	 */
	protected $regulation;

	/**
	 * @var Wages $wages
	 * Date: 2018/6/13
	 * @author George
	 */
	protected $wages;

	/**
	 * RegulationHandler constructor.
	 * @param Wages $wages
	 */
	public function __construct(Wages $wages)
	{
		$this->wages = $wages;
		// 获取基础数据ID
		$this->company_id = $wages->company_id;
		$this->social_security_id = $wages->social_security_id;
		$this->user_id = $wages->user_id;
		$this->pattern_id = $wages->pattern_id;
		$this->attendance_days = $wages->attendance_days;
		$this->holidays = $wages->holidays;
		$this->custom_fields = $wages->custom_fields;
	}

	/**
	 * 计算薪资规则
	 *
	 * Date: 2018/6/14
	 * @author George
	 * @return bool
	 */
	public function calculate()
	{
		// 根据薪资字段计算字段值
		foreach ($this->wages->custom_fields as &$field) {
			$fieldHandler = new FieldHandler($field);
			$field['value'] = $fieldHandler->handle();
		}
		// 更新薪资报表
		try {
			$this->wages->update(['custom_fields' => $this->custom_fields]);
			return true;
		} catch (Exception $exception) {
			Log::error($exception);
		}
	}
}
